package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.entity.Setmeal;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author wang
 * @since 2023-07-06
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Resource
    DishMapper dishMapper;

    @Override
    public Page<SetmealVO> pageSetmeal(Page<SetmealVO> setmealVOPage, SetmealPageQueryDTO setmealPageQueryDTO) {
        return baseMapper.pageSetmeal(setmealVOPage,setmealPageQueryDTO);
    }

    @Override
    public Result updateStatus(Integer status, Long id) {
        if (status == 1){
            List<Dish> dishList = dishMapper.setmealQueryDish(id);
            for (Dish dish : dishList) {
                if (dish.getStatus() == 0){
                    return Result.error("该套餐内含有未起售的菜品");
                }
            }
        }
        LambdaUpdateWrapper<Setmeal> setmealLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        setmealLambdaUpdateWrapper.eq(Setmeal::getId,id);
        setmealLambdaUpdateWrapper.set(Setmeal::getStatus,status);
        this.update(setmealLambdaUpdateWrapper);
        return Result.success(status == 1 ? "起售成功" : "停售成功");
    }

    //停售菜品时也要停售相关套餐
    @Override
    public void upStatus(Long id) {
        baseMapper.upStatus(id);
    }
}
